/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.makrobioklub.action.web;

import cz.makrobioklub.action.ejb.ActionRoles;
import java.util.Observable;
import java.util.Observer;
import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import org.fyx.mula.ejb.MulaLocal;
import org.fyx.mula.ejb.MulaRoles;
import org.fyx.core.web.event.MenuCreatingEventHandler;
import org.fyx.core.web.event.RoleCreatingEventHandler;
import org.fyx.linker.web.LinkerLocal;
import org.fyx.secure.entity.SecureRole;
import org.fyx.secure.ejb.SecureLocal;
import org.primefaces.component.menuitem.MenuItem;

/**
 * Mula observer pro naslouchani na vnitrni udalosti.
 * Zoodpovida za spravne provedeni akci na urcite udalosti.
 * 
 * @author xljan
 */
@Stateless
@LocalBean
public class ActionObserver implements Observer {

    @EJB
    private SecureLocal secure;
    @EJB
    private LinkerLocal linker;
    @EJB
    private MulaLocal mula;

    @Override
    public void update(Observable o, Object arg) {
        if (o instanceof MenuCreatingEventHandler) {
            menuCreate();
        } else if (o instanceof RoleCreatingEventHandler) {
            roleCreate();
        }
    }

    private void menuCreate() {
        MenuItem manageLanguage = new MenuItem();
        manageLanguage.setValue(mula.get("action_listAction"));
        manageLanguage.setIcon("ui-icon ui-icon-flag");
        manageLanguage.setUrl(linker.getLink("action/list.xhtml"));
        manageLanguage.setId("action_listAction");
        linker.addMenuItem(manageLanguage, ActionRoles.ACTION_WRITER);
    }

    private void roleCreate() {
        SecureRole languageManager = new SecureRole(ActionRoles.ACTION_WRITER);
        languageManager.setCategory("Action");
        languageManager.setTitle("Action manager");
        languageManager.setDescription("Creating and editing selfs action. "
                + "This role no grant right to edit forigen action.");
        secure.addRole(languageManager);
        
        languageManager = new SecureRole(ActionRoles.ACTION_READER);
        languageManager.setCategory("Action");
        languageManager.setTitle("Action reader");
        languageManager.setDescription("User can read action.");
        secure.addRole(languageManager);
        
        languageManager = new SecureRole(ActionRoles.ACTION_PUBLISHER);
        languageManager.setCategory("Action");
        languageManager.setTitle("Action publisher");
        languageManager.setDescription("This role grant right to publish "
                + "actions.");
        secure.addRole(languageManager);
        
        languageManager = new SecureRole(ActionRoles.ACTION_EDITOR);
        languageManager.setCategory("Action");
        languageManager.setTitle("Action editor");
        languageManager.setDescription("User can edit and delete all actions.");
        secure.addRole(languageManager);
    }
}
